package com.gopay.scheduler.mq.consumer;

import com.gopay.common.asynexport.service.AsynexportService;
import com.gopay.common.mq.consumer.AbstractMsgConsumer;
import com.gopay.common.mq.destination.Destinations;
import com.gopay.scheduler.job.AsyncExportJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

/**
 * 异步文件导出任务取消消息消费者
 * @author yu_liu6@hnair.com
 * @date 2018-04-27 11:38:49
 */
@Component
public class AsyncExportCancelConsumer extends AbstractMsgConsumer<String> {

    @Autowired
    private AsynexportService asynexportService;

    @Override
    public void doOnMessage(String msg) throws Exception {
        long id = Long.parseLong(msg);
        logger.info("【异步导出任务取消指令】接收到取消任务请求，任务id = {}", id);
        Future future = AsyncExportJob.getTaskMap().get(id);
        String result;
        if(null == future){
            result = asynexportService.cancelById(id);
        } else {
            result = future.cancel(true) ? "取消成功" : "取消失败";
        }
        logger.info("【异步导出任务取消指令】任务取消结果：{}", result);
    }

    @Override
    public void toCompleteInit() {
        setPubSubDomain(true);
        setDestination(Destinations.ASYNC_EXPORT_CANCEL_TOPIC);
    }
}
